const sqlite3 = require('sqlite3').verbose();
const path = require('path');

const dbPath = path.join(__dirname, 'treehole.db');
const db = new sqlite3.Database(dbPath);

const initDB = () => {
    db.serialize(() => {
        // 创建帖子表
        db.run(`CREATE TABLE IF NOT EXISTS posts (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            content TEXT NOT NULL,
            likes INTEGER DEFAULT 0,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP
        )`);

        console.log('✅ 数据库初始化完成');
    });
};

const getDB = () => db;

module.exports = { initDB, getDB };
